#include "Timer.hpp"

#ifdef _WIN32

#include <windows.h>

class QPCStopwatch : public IStopwatch
{
	LARGE_INTEGER last_start;
	long long total;

public:
	QPCStopwatch()
	{ reset(); }

	void reset()
	{ total = 0; }

	void start()
	{ QueryPerformanceCounter(&last_start); }

	void stop()
	{
		LARGE_INTEGER now;
		QueryPerformanceCounter(&now);
		total += now.QuadPart - last_start.QuadPart;
	}

	double getElapsed()
	{
		LARGE_INTEGER freq;
		QueryPerformanceFrequency(&freq);
		return total / (double) freq.QuadPart;
	}
};


#else

#include <time.h>

class CGTStopwatch : public IStopwatch
{
	timespec last_start;
	long long total;

public:
	CGTStopwatch()
	{ reset(); }

	void reset()
	{ total = 0; }

	void start()
	{ clock_gettime(CLOCK_MONOTONIC, &last_start); }

	void stop()
	{
		timespec now;
		clock_gettime(CLOCK_MONOTONIC, &now);
		total += (now.tv_sec - last_start.tv_sec) * 1000000000LL
			+ now.tv_nsec - last_start.tv_nsec;
	}

	double getElapsed()
	{
		return total / 1e9;
	}
};

#endif

IStopwatch * createStopwatch()
{
#if defined(_WIN32)
	return new QPCStopwatch();
#else
	return new CGTStopwatch();
#endif
}
